<template>
  <div>
    <h1>tab切换</h1>
    <el-tabs v-model="activeName">
      <el-tab-pane label="未读消息" name="first">
        <ul>
          <li v-for="item in list" :key="item.id" v-show="item.status == 0">
            <span> {{ item.title }}</span>
            <el-button style="float:right" @click="changea(item)"
              >标为已读</el-button
            >

            <el-divider></el-divider>
          </li>
        </ul>
        <el-button @click="change1">全部标为已读</el-button>
      </el-tab-pane>
      <el-tab-pane label="已读消息" name="second">
        <ul>
          <li v-for="item in list" :key="item.id" v-show="item.status == 1">
            <span> {{ item.title }}</span>
            <el-button style="float:right" @click="changeb(item)"
              >还原未读</el-button
            >
            <el-button style="float:right" @click="changec(item)"
              >删除</el-button
            >
            <el-divider></el-divider>
          </li>
        </ul>
        <el-button @click="change2">删除全部</el-button>
      </el-tab-pane>
      <el-tab-pane label="回收站" name="third">
        <ul>
          <li v-for="item in list" :key="item.id" v-show="item.status == 2">
            <span> {{ item.title }}</span>
            <el-button style="float:right" @click="changeb(item)"
              >还原</el-button
            >

            <el-divider></el-divider>
          </li>
        </ul>
        <el-button @click="change3">清空回收站</el-button>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
export default {
  data() {
    return {
      activeName: "first",
      list: [
        {
          id: 1,
          title: "事件1111",
          status: 0,
        },
        {
          id: 2,
          title: "事件2222",
          status: 0,
        },
        {
          id: 3,
          title: "事件333333",
          status: 1,
        },
        {
          id: 4,
          title: "事件44444",
          status: 1,
        },
        {
          id: 5,
          title: "事件5555",
          status: 2,
        },
        {
          id: 6,
          title: "事件6666",
          status: 2,
        },
      ],
    };
  },
  mounted() {
    if (localStorage.getItem("todolist")) {
      this.list = JSON.parse(localStorage.getItem("todolist"));
    }
  },

  methods: {
    save() {
      localStorage.setItem("todolist", JSON.stringify(this.list));
    },
    change1() {
      this.list.forEach((item) => {
        if (item.status == 0) {
          item.status = 1;
        }
      });
      this.save();
    },
    change2() {
      this.list.forEach((item) => {
        if (item.status == 1) {
          item.status = 2;
        }
      });
      this.save();
    },
    change3() {
      this.list.forEach((item, index) => {
        if (item.status == 2) {
          item.status = -1;
        }
      });
      this.save();
    },
    changea(item) {
      item.status = 1;
      this.save();
    },
    changeb(item) {
      item.status = 0;
      this.save();
    },
    changec(item) {
      item.status = 2;
      this.save();
    },
  },
};
</script>
